/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var map=null;
var map2=null;
var mgr;
var props=[];


function initializeMap() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.00, 36.00), 5);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMenuMapTypeControl());
        //map.addControl(new GMapTypeControl());
        map.enableScrollWheelZoom();
        map.enableContinuousZoom();

        //ADD OWERVIEW CONTROL
        var overV=new GOverviewMapControl(new GSize(150, 100));
        map.addControl(overV);
        
        //ADD MARKER MANAGER
        var mgrOptions = {
            trackMarkers: true
        };
        mgr = new MarkerManager(map,mgrOptions);

        //ADD EVENT LISTENERS
        GEvent.addListener(map, "moveend", function() {
            //var center = map.getCenter();
            
            });
        
        GEvent.addListener(map, "click", function() {
            
            });
        GEvent.addListener(map, "load", function() {
            
            });
        loadData("data.php");
        
        
    }
}

function initializeAddPropertyMap() {
    if (GBrowserIsCompatible()) {
        map2 = new GMap2(document.getElementById("map"));
        map2.setCenter(new GLatLng(39.00, 36.00), 4);
        map2.addControl(new GSmallMapControl());
        map2.addControl(new GMenuMapTypeControl());
        //map.addControl(new GMapTypeControl());
        map2.enableScrollWheelZoom();
        map2.enableContinuousZoom();
        
        var counter=0;
        var content="<p><b>Emlağınızın yerini haritanın üzerine tıklayarak işaretleyiniz...</b></p>";
        map2.openInfoWindowHtml(map2.getCenter(), "<div style=\"width:150px;height:50px;text-align: center;\">"+content+"</div>");
        
        GEvent.addListener(map2, "click", function(overlay,latlng) {
            
            if (latlng) {
                map2.clearOverlays();
                map2.addOverlay(createMarker(latlng));
                counter++;
                var lat = latlng.lat();
                var lon = latlng.lng();

                document.getElementById('lat').value=lat;
                document.getElementById('lng').value=lon;
                
            //document.getElementById('status').innerHTML = "Lat:"+lat+"Lon:"+lon;
                
            } else if (overlay instanceof GMarker) {
                // This code is never executed as the event listener is
                // removed the second time this event is triggered
                map2.removeOverlay(overlay);
                counter=0;
            }
            
            
        });
      
    }
}

function loadData(url) {
    mgr.clearMarkers();
    $('mapLayer').update('Harita Yukleniyor...');
    $('mapLayer').appear();
    
    GDownloadUrl(url, function(data, responseCode) {
        var batch = [];
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("property");
        for (var i = 0; i < markers.length; i++) {
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
            var price=markers[i].getAttribute("price");
            var id=markers[i].getAttribute("id");
            var type=markers[i].getAttribute("type");
            //var offerType=markers[i].getAttribute("offerType");
            var iconUrl="img/icon/red/marker";
            var marker=createPropertyMarker(iconUrl+(i+1)+".png",id,point,price,type,(i+1));
            //map.addOverlay(createPropertyMarker(point,price));
            batch.push(marker);
        }
        mgr.addMarkers(batch,4);
        mgr.refresh();
    });
    $('mapLayer').fade({ duration: 10.0 });
    
}


function createPropertyMarker(icon,id,posn,title,type,resultNo) {
    var tab1HTML;
    var tinyIcon = new GIcon();
    tinyIcon.image = icon;
    tinyIcon.iconSize = new GSize(20, 34);
    tinyIcon.shadowSize = new GSize(22, 20);
    tinyIcon.iconAnchor = new GPoint(6, 20);
    tinyIcon.infoWindowAnchor = new GPoint(5, 1);

    var marker = new GMarker(posn, {
        title: title,
        icon: tinyIcon
    //draggable:true
    });
    props.push(marker);
    marker.id=id;
    marker.resultNo=resultNo;
    GEvent.addListener(marker, 'click', function(id) {
        marker.openInfoWindow("<div style=\"width:300px; height:150px; text-align: center;\" id=\"propertyMarker"+marker.id+"\"></div>");
         
        if( (marker.resultNo % 2) == 0){
            $('result'+ marker.id).removeClassName('whitesmokeback');
            $('result'+ marker.id).addClassName('blueback');
        }else{
            $('result'+ marker.id).removeClassName('whiteback');
            $('result'+ marker.id).addClassName('blueback');
        }
         
         
        //Ajax Request for Bubble
        new Ajax.Request('marker.php?id='+marker.id ,
        {
            method:'get',
            onLoading: function(){
                $('propertyMarker'+marker.id).innerHTML="<div style=\"width:100%; text-align: center;\"><img src=\"img/loading.gif\"></div>";
            },
            onSuccess: function(transport){
                var response = transport.responseText || "no response text";
                $('propertyMarker'+marker.id).innerHTML=response;

            //$('result'+ marker.id).addClassName('selectedProperty');
            },
            onFailure: function(){
                alert('Something went wrong...');
            }
        });
       
        
    } );
    
    GEvent.addListener(marker, 'infowindowclose', function(id) {
        if( (marker.resultNo % 2) == 0){
            $('result'+ marker.id).toggleClassName('whitesmokeback');
        }else{
            $('result'+ marker.id).toggleClassName('whiteback');
        }
        
    });
    
    return marker;
}

function createMarker(posn) {
    var marker = new GMarker(posn, {
        //title: title,
        //icon: icon
        draggable:true
    });
    GEvent.addListener(marker, 'click', function() {
        
        } );
    
    return marker;
}

function showMarker(id){
    for (var i = 0; i < props.length; i++) {
      var marker=props[i];
      if(marker.id == id){
          marker.openInfoWindow("<div style=\"width:300px; height:150px; text-align: center;\" id=\"propertyMarker"+marker.id+"\"></div>");
          //Ajax Request for Bubble
        new Ajax.Request('marker.php?id='+marker.id ,
        {
            method:'get',
            onLoading: function(){
                $('propertyMarker'+marker.id).innerHTML="<div style=\"width:100%; text-align: center;\"><img src=\"img/loading.gif\"></div>";
            },
            onSuccess: function(transport){
                var response = transport.responseText || "no response text";
                $('propertyMarker'+marker.id).innerHTML=response;

            //$('result'+ marker.id).addClassName('selectedProperty');
            },
            onFailure: function(){
                alert('Something went wrong...');
            }
        });
      }
      
    }
}

function getMainMap(){
    
    var map=document.getElementById("map_canvas");
    return map;
}
